package com.dengyuanke.admin.controller;


import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.dengyuanke.common.result.JsonResult;
import com.dengyuanke.entity.Activity;
import com.dengyuanke.service.IActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 * 活动表 前端控制器
 * </p>
 *
 * @author dengyuanke
 * @since 2017-11-07
 */
@Api("活动相关API")
@RestController
public class ActivityController {

    @Autowired
    private IActivityService activityService;

    /**
     * 添加活动
     * @param activity
     * @return
     */
    @PostMapping("/addactivity")
    @ApiOperation(value = "添加活动",notes = "传入活动实体")
    public JsonResult addActivity(Activity activity){
        activity.setId(IdWorker.get32UUID());
        activity.setCreateTime(new Date());
        boolean insert = activityService.insert(activity);
        if(insert){
            return JsonResult.renderSuccess("添加成功");
        }else {
            return JsonResult.renderFail("添加失败");
        }
    }

    /**
     * 更新
     * @param activity
     * @return
     */
    @PostMapping("/updateactivity")
    @ApiOperation(value = "更新活动",notes = "传入带id的活动实体")
    public JsonResult updateActivity(Activity activity){
        if (StringUtils.isEmpty(activity.getId())){
            return JsonResult.renderFail("更新失败，传入实体未带id");
        }
        activity.setUpdateTime(new Date());
        boolean insert = activityService.updateById(activity);
        if(insert){
            return JsonResult.renderSuccess("更新成功");
        }else {
            return JsonResult.renderFail("更新失败");
        }
    }

    @PostMapping("/getactivities")
    @ApiOperation(value = "获得所有活动",notes = "不传入参数")
    public JsonResult getActivities(){
        return JsonResult.renderSuccess(activityService.selectList(null));
    }

    public static void main(String[] args) {
        System.out.println(System.getProperty("user.home"));
    }
}
